package com.yunzhong.appointment.illnesstpye.service.impl;

import com.yunzhong.appointment.entity.Illnessposition;
import com.yunzhong.appointment.entity.Person;
import com.yunzhong.appointment.illnesstpye.service.IllnessTypeService;
import com.yunzhong.appointment.mapper.IllnesspositionMapper;
import com.yunzhong.appointment.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class IllnessTypeServiceImpl implements IllnessTypeService {
    @Autowired
    private IllnesspositionMapper illnesspositionMapper;

    @Override
    public List<Illnessposition> query(PageData pd) {
        pd.setPageData(pd);
        return illnesspositionMapper.query(pd);
    }

    @Override
    public Illnessposition queryById(String id) {
        return illnesspositionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(Illnessposition illnessposition) {
        return illnesspositionMapper.insertSelective(illnessposition);
    }

    @Override
    public int edit(Illnessposition illnessposition) {
        return illnesspositionMapper.updateByPrimaryKeySelective(illnessposition);
    }

    @Override
    public String delete(String[] ids) {
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<ids.length;i++){
            //验证是否能删除，不能删除的需要把名称显示到页面上
            String str = illnesspositionMapper.validateDel(ids[i]);
            if (str != null && str.length() > 0) {
                String firstValue = str.substring(0, str.indexOf("@"));
                String zero = "0";
                if (zero.equals(firstValue)) {
                    illnesspositionMapper.deleteByPrimaryKey(ids[i]);

                } else {
                    String name = str.substring(str.indexOf("@") + 1);
                    sb.append(name).append(",");
                }
            } else {
                illnesspositionMapper.deleteByPrimaryKey(ids[i]);
            }
        }
        if (sb.length() > 0) {
            return sb.substring(0, sb.length() - 1);
        }
        return "";
    }

    @Override
    public List<Illnessposition> queryAll() {
        return illnesspositionMapper.queryAll();
    }

}
